package ro.gateway.aida.obj.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ro.gateway.aida.db.DBPersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.AIDAComponent;
import ro.gateway.aida.srv.IIDGenerator;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: AIDAComponentDB.java,v 1.1 2004/10/24 23:37:11 mihaipostelnicu Exp $
 */
public class AIDAComponentDB extends DBPersistenceManager
{
      private AIDAComponentDB( PersistenceToken token )
      {
            super( token );
      }

      public static AIDAComponentDB getManager( PersistenceToken token )
      {
            return new AIDAComponentDB( token );
      }

      public void insert(
            IIDGenerator idGenerators,
            AIDAComponent component )
            throws SQLException
      {

            Connection con = getConnection();
            if ( component.getId() == -1 )
            {
                  AIDAActivityObjectDB.getManager( token ).insert( idGenerators, component );
            }

            try
            {
                  PreparedStatement ps = con.prepareStatement( INSERT );

                  String[] langs = component.getAvailableLanguages();
                  for ( int i = 0; i < langs.length; i++ )
                  {
                        ps.clearParameters();
                        ps.setLong( 1, component.getId() );
                        ps.setString( 2, langs[ i ] );
                        ps.setString( 3, component.getName( langs[ i ] ) );
                        ps.executeUpdate();
                  }

                  ps.executeUpdate();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }
      }

      public AIDAComponent getById(
            long id )
            throws SQLException
      {

            Connection con = getConnection();
            AIDAComponent result = null;

            try
            {
                  PreparedStatement ps = con.prepareStatement( GET_BY_ID );
                  ps.setLong( 1, id );
                  ResultSet rs = ps.executeQuery();
                  while ( rs.next() )
                  {
                        if ( result == null )
                        {
                              result = new AIDAComponent();
                              result.setId( id );
                        }

                        String lang = rs.getString( 1 );
                        result.setName( lang, rs.getString( 2 ) );
                  }
                  rs.close();
                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            return result;
      }

      public void delete(

            long id )
            throws SQLException
      {

            Connection con = getConnection();

            try
            {
                  PreparedStatement ps = con.prepareStatement( DELETE );
                  ps.setLong( 1, id );
                  ps.executeUpdate();
                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }
      }

      public AIDAComponent[] getAllForActivity(
            long activity_id )
            throws SQLException
      {

            Connection con = getConnection();
            AIDAComponent[] result = null;
            ArrayList items = new ArrayList();

            try
            {
                  PreparedStatement ps = con.prepareStatement( GET_ALL_FOR_ACTIVITY );
                  ps.setLong( 1, activity_id );
                  ResultSet rs = ps.executeQuery();
                  AIDAComponent last_item = null;
                  while ( rs.next() )
                  {
                        long id = rs.getLong( 1 );
                        if ( ( last_item != null ) && ( last_item.getId() != id ) )
                              last_item = null;

                        if ( last_item != null )
                        {
                              String lang = rs.getString( 2 );
                              last_item.setName( lang, rs.getString( 3 ) );
                        }
                        else
                        {
                              last_item = new AIDAComponent();
                              last_item.setId( id );

                              String lang = rs.getString( 2 );
                              last_item.setName( lang, rs.getString( 3 ) );

                              items.add( last_item );
                        }
                  }
                  rs.close();
                  ps.close();
            }
            catch ( SQLException sqlEx )
            {
                  throw sqlEx;
            }
            finally
            {
                  con.close();
            }

            if ( items.size() > 0 )
            {
                  result = new AIDAComponent[ items.size() ];
                  items.toArray( result );
            }

            return result;

      }

      public static final String INSERT =
            "INSERT INTO act_components (id, lang, name) " +
            "VALUES (?,?,?)";

      public static final String GET_BY_ID =
            "SELECT lang, name " +
            "FROM act_components " +
            "WHERE id = ?";

      public static final String DELETE =
            "DELETE FROM act_components " +
            "WHERE id = ?";

      public static final String GET_ALL_FOR_ACTIVITY =
            "SELECT c.id, c.lang, c.name " +
            "FROM act_components c, objects o " +
            "WHERE o.activity_id=? AND o.id=c.id " +
            "ORDER BY c.id ASC, c.name ASC";
}
